package com.oro.util;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.oro.dao.StudentDao;
import com.oro.dao.UserDao;
import com.oro.domain.Authority;
import com.oro.domain.Categoria;
import com.oro.domain.Contatto;
import com.oro.domain.ContattoScuola;
import com.oro.domain.Corso;
import com.oro.domain.Costo;
import com.oro.domain.Email;
import com.oro.domain.IndirizzoAU;
import com.oro.domain.IndirizzoIT;
import com.oro.domain.Iscrizione;
import com.oro.domain.MacroCategoria;
import com.oro.domain.Scuola;
import com.oro.domain.Sede;
import com.oro.domain.Studente;
import com.oro.domain.Telephone;
import com.oro.domain.User;

public class InitBean {

	private static final Logger logger = Logger.getLogger(InitBean.class);
	@Autowired
	StudentDao studentDao;

	@Autowired
	UserDao userDao;

	private Logger getLogger() {
		return logger;
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	@PostConstruct
	public void postConstruct() {

		try {
			getLogger().info("InitBean has been constructed.");

			getLogger().info("Adding the predefined authorities");

			userDao.save(Authority.ADMIN);
			userDao.save(Authority.ANONYMOUS);
			userDao.save(Authority.GUEST);
			userDao.save(Authority.OPERATOR);
			userDao.save(Authority.STUDENT);
			userDao.save(Authority.SUPERADMIN);
			userDao.save(Authority.USER);

			getLogger().info("Adding the predefined users");

			User admin = new User("admin", "nu66et$", Authority.ADMIN);
			User superadmin = new User("superadmin", "nu66et$",
					Authority.SUPERADMIN);

			userDao.save(admin);
			userDao.save(superadmin);

			getLogger().info("Adding some macro categories");

			studentDao.save(MacroCategoria.ENGLISH);
			studentDao.save(MacroCategoria.VOCATIONAL);
			studentDao.save(MacroCategoria.PROFESSIONAL);
			studentDao.save(MacroCategoria.CERTIFICATION);
			studentDao.save(MacroCategoria.TAFE);
			studentDao.save(MacroCategoria.UNIVERSITY);

			getLogger().info("Adding some categories");

			studentDao.save(Categoria.AGED_CARE_CERT_I);
			studentDao.save(Categoria.AGED_CARE_CERT_II);
			studentDao.save(Categoria.AGED_CARE_CERT_III);
			studentDao.save(Categoria.BAR_ENGLISH);
			studentDao.save(Categoria.BAR_SKILLS);
			studentDao.save(Categoria.CAMBRIDGE);
			studentDao.save(Categoria.CAMBRIDGE);
			studentDao.save(Categoria.CCENT);
			studentDao.save(Categoria.CCNA);
			studentDao.save(Categoria.GENERAL_ENGLISH);
			studentDao.save(Categoria.GENERAL_ENGLISH_BEGINNER);
			studentDao.save(Categoria.GENERAL_ENGLISH_INTERMEDIATE);
			studentDao.save(Categoria.GENERAL_ENGLISH_ADVANCED);
			studentDao.save(Categoria.IELTS);
			studentDao.save(Categoria.RSA);
			studentDao.save(Categoria.VET_PROFESSIONAL);
			studentDao.save(Categoria.ELICOS_ENGLISH);
			studentDao.save(Categoria.NON_ELICOS_ENGLISH);

			getLogger().info("Adding a student. (Marzocchino Rossittatti)");

			Studente marzocchino = new Studente("Marzocchino", "Rossittatti");
			marzocchino.add(new Email("marcorossi@gmail.com",
					Email.TYPE_PERSONAL));
			marzocchino.add(new Telephone("3335858585", Telephone.TYPE_MOBILE));
			marzocchino.add(new Telephone("024747474", Telephone.TYPE_HOME));
			marzocchino.add(new Telephone("069595959", Telephone.TYPE_WORK));
			IndirizzoIT marzocchinoindit = new IndirizzoIT("via Roma, 28",
					"Milano", "Italy");
			IndirizzoAU marzocchinoindau = new IndirizzoAU("via Roma, 28",
					"Milano", "Italy");
			marzocchinoindit.setProvincia("MI");
			marzocchinoindit.setCap("20200");
			marzocchino.getIndirizziIt().add(marzocchinoindit);
			// marzocchino.getIndirizziAu().add(marzocchinoindau);
			studentDao.save(marzocchino);

			getLogger().info("Adding a student. (Mariollo Rossettilli)");

			Studente mario = new Studente("Mariollo", "Rossettilli");
			mario.add(new Email("mariorrossetti@gmail.com", Email.TYPE_PERSONAL));
			mario.add(new Email("mariorrossetti@hotmail.com",
					Email.TYPE_PERSONAL));
			mario.add(new Email("mariorrossetti@fastmail.com",
					Email.TYPE_PERSONAL));
			mario.add(new Telephone("3333333333", Telephone.TYPE_MOBILE));
			mario.add(new Telephone("025728391", Telephone.TYPE_HOME));
			mario.add(new Telephone("13635838172", Telephone.TYPE_WORK));
			IndirizzoIT marioindit = new IndirizzoIT("via Piola, 27", "Milano",
					"Italy");
			marioindit.setProvincia("MI");
			marioindit.setCap("20200");
			mario.getIndirizziIt().add(marioindit);
			studentDao.save(mario);

			getLogger().info("Adding a student. (Ettoresdo Burinizzu)");

			Studente ettore = new Studente("Ettoresdo", "Burinizzu");
			ettore.setCitizenship("Italian");
			ettore.add(new Email("ettoreburini@gmail.com", Email.TYPE_PERSONAL));
			ettore.add(new Telephone("3333333333", Telephone.TYPE_MOBILE));
			ettore.add(new Telephone("025728391", Telephone.TYPE_HOME));
			ettore.add(new Telephone("13635838172", Telephone.TYPE_WORK));
			IndirizzoIT ettoreindit = new IndirizzoIT("via Piola, 27", "Roma",
					"Italy");
			ettoreindit.setProvincia("RM");
			ettoreindit.setCap("20100");
			ettore.getIndirizziIt().add(ettoreindit);

			Contatto ec1 = new Contatto("Pinuzzo", "Arguizi");
			ec1.setCitizenship("Italian");
			ec1.setRelazione("Amico");
			ec1.add(new Email("pinuzzp@hhh.com", Email.TYPE_LEISURE));
			ec1.add(new Telephone("3333333344", Telephone.TYPE_HOME));
			ec1.add(new Telephone("6548798789", Telephone.TYPE_FAX));
			ec1.getIndirizziIt().add(
					new IndirizzoIT("via Roma 2345", "Empoli", "Empoli",
							"23456", "Italy"));
			ettore.getContattiDiEmergenza().add(ec1);

			studentDao.save(ettore);

			getLogger().info("Adding a school (Elsisaccia)");

			Scuola elsis = new Scuola("Elsisaccia");
			IndirizzoAU elsisind = new IndirizzoAU("12, George St", "Sydney",
					"2121", IndirizzoAU.NSW, "Australia");
			elsis.setIndirizzo(elsisind);
			ContattoScuola john = new ContattoScuola("John", "Smith");
			john.setSchool(elsis);
			ContattoScuola rey = new ContattoScuola("Rey", "Pak");
			rey.setSchool(elsis);
			elsis.add(john);
			elsis.add(rey);
			Sede elsis1 = new Sede();
			elsis1.setContact(john);
			john.getBranches().add(elsis1);
			elsis1.setName("Elsisaccia 1");
			elsis1.setPrincipale(true);
			elsis1.setIndirizzo(elsisind);
			elsis1.setSchool(elsis);
			elsis.add(elsis1);
			Sede elsis2 = new Sede();
			elsis2.setContact(rey);
			rey.getBranches().add(elsis2);
			elsis2.setName("Elsisaccia 2");
			elsis2.setPrincipale(true);
			elsis2.setIndirizzo(new IndirizzoAU("1s2, Superman St",
					"Melbourne", "1234", IndirizzoAU.Vic, "Australia"));
			elsis2.setSchool(elsis);
			elsis.add(elsis2);
			Corso c1 = new Corso("General English 1", "GE1",
					Categoria.GENERAL_ENGLISH);
			Corso c2 = new Corso("General English 2", "GE2",
					Categoria.GENERAL_ENGLISH);
			Corso c3 = new Corso("Advanced General Enhlish", "AGD",
					Categoria.GENERAL_ENGLISH_ADVANCED);
			Corso c4 = new Corso("General english for beginners", "GEFB",
					Categoria.GENERAL_ENGLISH_BEGINNER);
			c1.setSchool(elsis);
			c2.setSchool(elsis);
			c3.setSchool(elsis);
			c4.setSchool(elsis);
			elsis.getCourses().add(c1);
			elsis.getCourses().add(c2);
			elsis.getCourses().add(c3);
			elsis.getCourses().add(c4);
			c1.add(elsis1);
			c2.add(elsis1);
			c3.add(elsis1);
			c4.add(elsis1);
			elsis1.add(c1);
			elsis1.add(c2);
			elsis1.add(c3);
			elsis1.add(c4);
			c1.add(elsis2);
			c2.add(elsis2);
			elsis2.add(c1);
			elsis2.add(c2);
			studentDao.save(elsis);
			// studentDao.save(elsis1);
			// studentDao.save(elsis2);
			// studentDao.save(c1);
			// studentDao.save(c2);
			// studentDao.save(c3);
			// studentDao.save(c4);

			getLogger().info("Adding a school (ACASXYZ)");

			Scuola acas = new Scuola("ACASXYZ");
			IndirizzoAU acasind = new IndirizzoAU("14, Kent St", "Sydney",
					"2121", IndirizzoAU.NSW, "Australia");
			acas.setIndirizzo(acasind);
			Sede acas1 = new Sede();
			acas1.setName("Acasxyz 1");
			acas1.setPrincipale(true);
			acas1.setIndirizzo(acasind);
			acas1.setSchool(acas);
			acas.add(acas1);
			Corso c5 = new Corso("Acas General English 1", "acasGE1",
					Categoria.GENERAL_ENGLISH);
			c5.getCosti().add(new Costo(4, 10, 123, c5));
			Corso c6 = new Corso("Acas General English 2", "acasGE2",
					Categoria.GENERAL_ENGLISH);
			Corso c7 = new Corso("Acas Advanced General Enhlish", "acasAGD",
					Categoria.GENERAL_ENGLISH_ADVANCED);
			Corso c8 = new Corso("Acas General english for beginners",
					"acasGEFB", Categoria.GENERAL_ENGLISH_BEGINNER);
			c5.setSchool(acas);
			c6.setSchool(acas);
			c7.setSchool(acas);
			c8.setSchool(acas);
			acas.getCourses().add(c5);
			acas.getCourses().add(c6);
			acas.getCourses().add(c7);
			acas.getCourses().add(c8);
			c5.add(acas1);
			c6.add(acas1);
			c7.add(acas1);
			c8.add(acas1);
			acas1.add(c5);
			acas1.add(c6);
			acas1.add(c7);
			acas1.add(c8);
			studentDao.save(acas);
			// studentDao.save(acas1);
			// studentDao.save(c5);
			// studentDao.save(c6);
			// studentDao.save(c7);
			// studentDao.save(c8);

			getLogger().info("Adding some enrollments.");

			studentDao.save(new Iscrizione(mario, c1));
			studentDao.save(new Iscrizione(mario, c2));
			studentDao.save(new Iscrizione(mario, c3));
			studentDao.save(new Iscrizione(mario, c4));
			studentDao.save(new Iscrizione(marzocchino, c1));
			studentDao.save(new Iscrizione(marzocchino, c2));
			studentDao.save(new Iscrizione(ettore, c5));
			studentDao.save(new Iscrizione(ettore, c6));
			studentDao.save(new Iscrizione(ettore, c7));
			studentDao.save(new Iscrizione(ettore, c8));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@PreDestroy
	public void preDestroy() {
		getLogger().info("InitBean is being destroyed.");
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
